/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.edu.agh.student.nn.core.cp;

import pl.edu.agh.student.nn.core.*;

/**
 * @author janburkot
 */
public class GrossbergLayer extends Layer {
    //private TeachingParameters teachingParameters;
    private Teaching grossbergTeaching;

    public GrossbergLayer(int size, LayerType type) {
        super(size, type);
        switch (type) {
            case WH:
                grossbergTeaching = new WidowHoffTeaching();
                break;
            case DELTA:
                grossbergTeaching = new DeltaRuleTeaching();
                break;
        }
    }

    @Override
    public void learn(int currentIteration, TrainingSet trainingSet) {
        for (int j = 0; j < getNeurons().size(); j++) {
            InputOutputNeuron grossbergNeuron = (InputOutputNeuron) getNeurons().get(j);

            for (int i = 0; i < grossbergNeuron.getInputConnections().size(); i++) {
                Connection inConnection = grossbergNeuron.getInputConnections().get(i);
                double oldWeight = inConnection.getWeight();
                double alpha = getDoubleParam(LayerParameter.ALPHA, currentIteration);
                double expectedResult = trainingSet.getExpected()[j];
                double newWeight = grossbergTeaching.countNewWeight(
                        oldWeight,                                      // old weight
                        alpha,                                          // learning rate
                        expectedResult,                                 // expected result
                        grossbergNeuron.getOutput(),                    // output
                        inConnection.getFromNeuron().getOutput());      // input
                
                //System.out.println("X: "+(expectedResult - neuron.getOutput()));
                inConnection.setWeight(newWeight);
            }

        }
    }

    public void setActivationFunction(ActivationFunction function) {
        ((DeltaRuleTeaching) grossbergTeaching).setF(function);
    }
}
